﻿$(document).ready(function(){
	$(".add_more_btn").click(function(){
		$(".add_more_btn").hide();
		$(".add_more_content").fadeIn();
	})
	$(".close_more_btn").click(function(){
		$(".add_more_content").fadeOut();
		$(".add_more_btn").show();
	})

});

function ChangeListName(){



}
function show_upimg(y){
	
	var img_div="\#"+"upload_img"+y;
	if($(img_div).is(":visible")){
		$(img_div).fadeOut(200);
	}else{
	
		$(img_div).fadeIn(200);
	}

}

function showmore(y){
var more_div="\#more_div"+y;
if($(more_div).is(":visible")){
$(more_div).fadeOut(500);
}else{
$(more_div).fadeIn(500);

}





}

x_list(1);
var x;
function x_list(data){
  x = data;  
}
y_list(1);
var y;
function y_list(data){
  y = data;  
}

z_list(0);
var z;
function z_list(data){
  z = data;  
}
w_list(0);
var w;
function w_list(data){
  w = data;  
}

//增加
function addmode(){
		if(x<2){
		
			x++;
			$("#mode_list").append(rethml);
			var id="\#"+"mode"+x;
			$(id).fadeIn();
			add_mode_connect(x,y);
			if(x==2){
				w=1;
			}
		}else if(w==1){
			addmore();
			x=2;
			z=1;
		}
	
		
			function rethml(){
			var str='<li Style="display:none;" id="mode'+x+'"><textarea name="mode'+x+'"></textarea><input name="mode'+x+'" type="button" onclick="remove_a('+x+')" style="background:url(../images/detele2.gif);width:17px;height:16px;border:0px;"></li>';
			return str;
		}
}


 function addmore(){
 
	 if(w==1){
		 for(var i=1;i<=y;i++){
		 
		var more="\#List_Form"+i;


		var li_div="\#more"+i;
		var my_li=$(li_div);

				if(my_li.length==0){
					str='<div class="input_content" id="more'+i+'">\n\
							<ul>\n\
								<li class="add_more_btn" onclick="showmore('+i+')">more...</li>\n\
							</ul>\n\
						</div>';
					$(more).after(str);
				}
		}
	}
} 


function addcolnum(){

y++;              
$(".list_content").append(rethml);
var idd="\#"+"content"+y;
$(idd).fadeIn(800);
add_mode_connect(x,y);//////x以加加

if(z==1){
addmore();
}
function rethml(){

	var str='		<div class="content1" id="content'+y+'" style="display:none">\n\
						<div class="rank_pic">\n\
							<div class="rank_num" id="rank'+y+'">\n\
								<span>'+y+'</span>\n\
							</div>\n\
							<div class="pic_1" id="pic'+y+'" onclick="show_upimg('+y+')">\n\
								<img style="width:44px;height:44px;"src="../images/p3.gif"/>\n\
							</div>\n\
						</div>\n\
						<div class="un_useful_11">\n\
						<form  method="post"  action="" id="List_Form'+y+'">\n\
							<div class="input_content">\n\
								<ul id="ul'+y+'">\n\
								</ul>\n\
							</div>\n\
						</form>\n\
							<div class="detele_list" >\n\
								<input type="button" id="delete'+y+'" onclick="delete_column('+y+')" name="delete'+y+'"style="background:url(../images/detele_list.png);width:52px;height:15px;border:0px;">\n\
							</div>\n\
							</div>\n\
						<div class="add_more_content" id="more_div'+y+'">\n\
							<textarea name="more_text'+y+'"></textarea>\n\
							<div class="add_content_btn">\n\
								<div class="save_more_btn">\n\
									<input type="button" onclick="showmore('+y+')"style="width:47px;height:21px;background:url(../images/save_more_btn.jpg);"/>\n\
								</div>\n\
							</div>\n\
						</div>\n\
						<!--上传图片-->\n\
						<div class="add_pic" id="upload_img'+y+'">\n\
							<div class="upload_pic">\n\
								<form action="img_upload.php" enctype="multipart/form-data" target="upframe'+y+'"	name="img_form_sub'+y+'" method="post">\n\
									<span>URL:</span>\n\
									<input type="file" name="img_'+y+'" style="color:#000;" />\n\
									<input type="hidden" value="'+y+'" name="img_id">\n\
									<input type="submit" value="" style="background:url(../images/sure_upload_pic.png);width:62px;height:25px;border:0px;" onclick="sbumit_form('+y+')" name="sub_'+y+'"/>\n\
								</form>\n\
							</div>\n\
							<div class="close_upload_pic">\n\
								<input type="button" onclick="show_upimg('+y+')" style="width:47px;height:21px;background:url(../images/save_more_btn.jpg)"/>\n\
							</div>\n\
						</div>	\n\
						<iframe class="upframe" name="upframe'+y+'" src="img_upload.php" style="display:none;"> </iframe>\n\
				</div>';

			return str;
		}


}
									

function add_mode_connect(xx,yy){
 if(xx>2){
	xx=2;
 }
var i;
for(i=1;i<=yy;i++){
			for(j=1;j<=xx;j++){
				var  Class="\#"+"text"+i+"_"+j;
				var my_element=$(Class);
				
				if(my_element.length==0){
				var form="\#"+"ul"+i;
					$(form).append(rethml(i,j));
					var id="\#"+"text"+i+"_"+j;
					$(id).fadeIn(800);
				}
				
				
			}
	}
}
function rethml(i,j){
			
			var str='<li id="text'+i+'_'+j+'"><textarea name="text'+i+'_'+j+'"></textarea></li>';
			return str;
}
//删除
////移除并替换类别//////
function remove_a(id){

var dd="\#"+"mode"+id;
$(dd).remove()
var i;
for(i=(id+1);i<=x;i++){
	var dd="\#"+"mode"+i;
	var jian="mode"+(i-1); 
	$(dd).attr("id",jian);
		var input_name="textarea[name="+"'mode"+i+"']";
		var rep_inp_name="mode"+(i-1);
		$(input_name).attr("name",rep_inp_name);
	var span_name="input[name="+"'mode"+i+"']";
	$(span_name).remove()			//////移除--X
	var ddd="\#"+"mode"+(i-1);			
	$(ddd).append(rethml(i-1));		///////修改添加--X
 	
} 
function rethml(a){
			var str='<input type="button" onclick="remove_a('+a+')" style="background:url(../images/detele2.gif);width:17px;height:16px;border:0px;">';
			return str;
		} 
	
remove_sort(id)

}

function remove_sort(id){
var i;
for(i=1;i<=y;i++){
var dd="\#"+"text"+i+"_"+id;

$(dd).remove()

}


replace_list(id);
}
 ////替换前面的
function replace_list(remove_id){

for(i=1;i<=y;i++){
			for(j=(remove_id+1);j<=x;j++){
				var  Class="\#"+"text"+i+"_"+j;
				var rep="text"+i+"_"+(j-1);
				$(Class).attr("id",rep);
				var input_name="textarea[name="+"'text"+i+"_"+j+"']";
				var rep_inp_name="text"+i+"_"+(j-1);
				$(input_name).attr("name",rep_inp_name);
				
				
			}
	}
	
x--;	
}

function delete_column(delete_id){
 var delete_column="\#"+"content"+delete_id;
$(delete_column).remove(); 

var i;

for(i=(delete_id+1);i<=y;i++){		////y已经加一了


 var column="\#"+"content"+i;			///////替换column
var rep_column="content"+(i-1);
$(column).attr("id",rep_column);
		var rank="\#"+"rank"+i;
		$(rank).find("span").html(i-1);
		var rep="rank"+(i-1);
		$(rank).attr("id",rep);
		
		 
		 
	var img="\#"+"pic"+i;	///////替换img
	$(img).attr({"onclick":"show_upimg("+(i-1)+")","id":"pic"+(i-1)+""});
	

				var img_form="\#"+"List_Form"+i;		///////替换List_Form6
				var rep_img_form="List_Form"+(i-1);
				$(img_form).attr("id",rep_img_form);
				
				var delet="\#"+"delete"+i;
				$(delet).attr({"id":"delete"+(i-1)+"","onclick":"delete_column("+(i-1)+")"});
				
				var ul="\#ul"+i;
				$(ul).attr("id","ul"+(i-1)+"");
				
				for(j=1;j<=x;j++){						///////替换input
					var  Class="\#"+"text"+i+"_"+j;
					var rep="text"+(i-1)+"_"+j;
					$(Class).attr("id",rep);
					var input_name="textarea[name="+"'text"+i+"_"+j+"']";
					var rep_inp_name="text"+(i-1)+"_"+j;
					$(input_name).attr("name",rep_inp_name);
				}
				//替换图片上传区
				var img_form="input[name="+"'img_"+i+"']";
				$(img_form).next().attr("value",(i-1));
				
				$(img_form).attr("name","img_"+(i-1));
				var img_form_sub="input[name="+"'sub_"+i+"']";
				
				
				$(img_form_sub).attr({"name":"sub_"+(i-1),"onclick":"sbumit_form("+(i-1)+")"});
				var img_form="form[name="+"'img_form_sub"+i+"']";
				$(img_form).attr({"name":"img_form_sub"+(i-1)+"","target":"upframe"+(i-1)+""});
				var up_img="\#upload_img"+i;
				$(up_img).find("form").attr({"name":"img_form_sub"+(i-1)+"","target":"upframe"+(i-1)+""})
				
			
				
				
				$(up_img).attr("id","upload_img"+(i-1)+"");
				var iframe="iframe[name="+"'upframe"+i+"']";
				$(iframe).attr("name","upframe"+(i-1));
				
				
				
			if(w==1){
			var more="\#more"+i;
			$(more).attr("id","more"+(i-1)).find("ul").find("li").attr("onclick","showmore("+(i-1)+")");
			var more_div="\#more_div"+i;
			$(more_div).attr("id","more_div"+(i-1)).find("textarea").attr("name","more_text"+(i-1));
			
			
			}	


}
y--;
}

//图片上传
function sbumit_form(id){

var img_='form[name="img_form_sub'+id+'"]'; 

		$(img_).ajaxForm({
		 url:"../public_files/img_upload.php",  
		 cache:false,
		 type :"POST",
		 data:"img_id"+id,
		 dataType: "JSON",
		success:function(data){
				
			 var ss="\#"+"pic"+id;
				
					$(ss).find("img").attr("src","../"+data); 
			show_upimg(id)					
		}

		});
}



 //完成榜单提交
function get_count(status){
 var listname=$("#listname").text();
var introduction=$("#introduce").val();
//var sort_id=$("#choose_sort").val(); 
var sort_id=1;
for(var i=1;i<=y;i++){
	var listname_id=i;
	var img="\#"+"pic"+i;
	var img_path=$(img).find("img").attr("src");
	var list_form="\#"+"List_Form"+i;
	var form_data=$(list_form).serialize();	
	form_data = decodeURIComponent(form_data,true);
	var sort_data=$("#mode").serialize();
	sort_data = decodeURIComponent(sort_data,true);
	
	//alert(img_path);
	
 	if(z==1){
	
	var more="textarea[name="+"'more_text"+i+"']";
	var more_content=$(more).val();
	
 	$.ajax({
		type: "POST",
		url: "../music/action_create.php",
		data: form_data+"&"+sort_data+"&x="+x+"&y="+i+"&listname_id="+listname_id+"&listname="+listname+"&introduction="+introduction+"&img_path="+img_path+"&sort_id="+sort_id+"&status="+status+"&more="+more_content,
		dataType:"text",
		success: function(data){
			var a=data.split("|");
				window.location.href="http://localhost/last/8.15/once_again/music/MusicListContent.php?listname_id="+a[1]+"&type=1&sort_id="+a[0];
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			alert(XMLHttpRequest+textStatus+errorThrown);
			}	
	}); 
	
	
	
	
	}else{
  	$.ajax({
		type: "POST",
		url: "action_create.php",
		data: form_data+"&"+sort_data+"&x="+x+"&y="+i+"&listname_id="+listname_id+"&listname="+listname+"&introduction="+introduction+"&img_path="+img_path+"&sort_id="+sort_id+"&status="+status,
		dataType:"html",
		success: function(data){
				var a=data.split("|");
				window.location.href="http://localhost/last/8.15/once_again/music/MusicListContent.php?listname_id="+a[1]+"&type=1&sort_id="+a[0];
	
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			alert(XMLHttpRequest+textStatus+errorThrown);
			}	
	}); 
	} 
	} 
	
	


}

function DeleteList(){

window.location.href="creat_list.php";

} 



